package com.example.tired1.guidepro.model;

import com.example.tired1.guidepro.bean.WeatherResult;
import com.example.tired1.guidepro.iface.WeatherInface;
import com.example.tired1.guidepro.listener.WeatherListener;
import com.example.tired1.guidepro.service.WeatherService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by tired1 on 2017/3/19.
 */

public class WeatherModel implements WeatherInface {
    private static final String BASE_URL="http://www.weather.com.cn/data/";
    private Retrofit retrofit;
    private WeatherService service;

    @Override
    public void getWeather(String cityNumber,final WeatherListener listener) {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service=retrofit.create(WeatherService.class);
        Call<WeatherResult>call=service.getResult(cityNumber);
        call.enqueue(new Callback<WeatherResult>() {
            @Override
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if(response.isSuccessful() && response.body()!= null){
                    listener.onResponse(response.body().getWeatherinfo());
                }

            }

            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                listener.onFailure(t.toString());

            }
        });

        
    }
}
